<template>
  <div class="flowPower">
  </div>
</template>
<script>
// import { BaseCompView } from "../../base/BaseCompView";
import { bbglPrint } from "../api/module/bbgl/bbgl";
import {
  getList as getWordFilesList,
  getDownloadUrl,
  getDownloadZBUrl,
  downWordFile
} from "../api/common/wordfiles";
import {
  getList as getFileDataList,
  getDownloadUrl as getFileDataDownloadUrl,
  convertOFD
} from "../api/common/filedata";
import {
  getOpenList,
  getOpenDownloadUrl,
} from "../api/common/attachment";
import { buildView } from '../api/common/moduleview'
export default {
  name: "FlowPowerMethod",
  // extends: BaseCompView,
  props: [
    "docunid"
  ],
  components: {
  },
  data() {
    return {
      readOnly: true,
      isCurrentFlow: false,
      printLoading: false,
      hasGaiZhangDoc: false,
      hasDoc: false,
      isPrinter: false,
      isGwFj: false,
      isYyy: false,
      sealData: [],
      baseURL: localStorage.getItem("local_server_ip") || window.HC_config.VUE_APP_BASE_API || process.env.VUE_APP_BASE_API,
    };
  },
  mounted() {
    //提供全局方法以供调用
    //查看正文
    //公文传输查看正文
    window.gwcsCkzw = this.gwcsCkzw;
    window.writeDoc = this.writeDoc;
    window.writeZhiBanDoc = this.writeZhiBanDoc;

    //下载正文
    window.downloadDoc = this.downloadDoc;
    //盖章
    window.ZhuanBanGaiZhang = this.ZhuanBanGaiZhang;
    //查看盖章正文
    window.gaiZhangDoc = this.gaiZhangDoc;
    //下载盖章正文
    window.downloadGaiZhang = this.downloadGaiZhang;
    //打开指定正文
    window.openRawZw = this.openRawZw;
    //打开指定改稿正文
    window.openGGZw = this.openGGZw;

    var element = document.createElement('script')
    element.src = '/officeplugin2/js/CustomDocFunctionV2.js'
    document.body.appendChild(element)
    var element2 = document.createElement('script')
    element2.src = '/officeplugin2/js/wps.js'
    document.body.appendChild(element2)
    var element_et = document.createElement("script");
    element_et.src = "/officeplugin2/js/et.js";
    document.body.appendChild(element_et);
    var element3 = document.createElement('script')
    element3.src = '/officeplugin2/js/wps_sdk.js'
    document.body.appendChild(element3)

    // this.getSealData()

  },
  methods: {
    getSealData() {
      // 获取印章数据
      let params = {
        key: 100,
        moduleName: 'yzgl',
        viewType: 'yzgl',
        pageSize: 10,
        pageNum: 1,
        queryMap: JSON.stringify({}),
        paramsMap: JSON.stringify({})
      }
      buildView(params)
        .then((res) => {
          if (res.code == 200) {
            if (res.data.tableList.code == 200) {
              res.data.tableList.result.data.map(item => {
                // item.attachmentUrl = this.$define.PROJECT[this.$define.DEFAULT_PROJECT]
                //       .VUE_APP_API_BASE_URL + '/attachment/api'
                this.getAttachments(item)
              })
              this.sealData = res.data.tableList.result.data
              console.log(this.sealData, '印章数据')
            } else {
              this.sealData = []
            }
          }
        }).catch(err => {
          console.log(err)
        })
    },
    getAttachments(item) {
      let param = {
        relationUnid: item.unid,
        moduleName: 'yzgl',
        type: 'yzgl'
      }
      getOpenList(param).then(res => {
        console.log(res, '附件')
        if (res.code == 200) {
          if (res.result.data.length > 0) {
            // var imgUrl = getOpenDownloadFile(res.result.data[0].fileName)
            var imgUrl = getOpenDownloadUrl(res.result.data[0].unid)
            console.log(imgUrl)
            item.imgUrl = imgUrl
          } else {
            item.imgUrl = ''
          }
        }
      }).catch(err => {
        console.log(err)
      })
    },
    //下载盖章正文
    downloadGaiZhang() {
      var openurl = getFileDataDownloadUrl(this.docunid);
      window.open(openurl);
    },
    //转版盖章
    ZhuanBanGaiZhang() {
      //转版盖章

      convertOFD(this.docunid)
        .then(res => {
          if (res.code == 200) {
            var that = this;
            var windowObj = window.open(
              "/zxfw/ofdgz.html?unid=" + this.docunid
            );
            var loop = setInterval(function () {
              if (windowObj != null && windowObj.closed) {
                clearInterval(loop);
                //刷新盖章按钮
                // that.$router.go(0)
                // that.getGaiZhangDoc()
                //直接刷新页面
                window.location.reload();
              }
            }, 800);
          }
        })
        .catch(err => {
          this.$message.error("系统错误");
        });
    },
    //打开pdf盖章文件
    linktoGaiZhangDoc() {
      // window.open("/showpdf?unid=1486215916641755138" + this.docunid)
      //let _url='/showpdf?type=attachment&unid=1486215916641755138'
      let _url = "/showpdf?type=attachment&unid=" + this.docunid;
      window.location.href = _url;
    },
    //查看盖章文件
    gaiZhangDoc() {
      if (typeof this.docunid == "undefined") {
        return;
      }
      //获取盖章文件
      getFileDataList({
        parentUnid: this.docunid
      })
        .then(res => {
          if (res.code == 200) {
            if (res.result && res.result.data.length > 0) {
              //存在，打开fileData盖章文伯
              var item = res.result.data[res.result.data.length - 1];
              // viewDocumentWithFileData(
              //         this.$define.PROJECT[this.$define.DEFAULT_PROJECT]
              //           .VUE_APP_API_BASE_URL,
              //         item.unid,
              //         ""
              //       );

              var filePath =
                item.efileHastmFilepath == null
                  ? item.efileHasstampFilepath
                  : item.efileHastmFilepath;
              var fileName =
                item.efileHastmFilename == null
                  ? item.efileHasstampFilename
                  : item.efileHastmFilename;
              if (
                filePath.toLowerCase().indexOf(".ofd") != -1 ||
                filePath.toLowerCase().indexOf(".pdf") != -1 ||
                filePath.toLowerCase().indexOf(".gd") != -1 ||
                fileName.toLowerCase().indexOf(".ofd") != -1 ||
                fileName.toLowerCase().indexOf(".pdf") != -1 ||
                fileName.toLowerCase().indexOf(".gd") != -1
              ) {
                if (
                  window.HC_config.VUE_APP_OPEN_OFD
                ) {
                  // ofd文件用福昕打开
                  window.open(
                    "/zxfw/foxitofd.html?unid=" +
                    this.docunid +
                    "&type=filedata"
                  );
                } else {
                  if (
                    filePath.toLowerCase().indexOf(".ofd") != -1 ||
                    filePath.toLowerCase().indexOf(".gd") != -1 ||
                    fileName.toLowerCase().indexOf(".ofd") != -1 ||
                    fileName.toLowerCase().indexOf(".gd") != -1
                  ) {
                    //ofd文件用数科打开
                    window.open("/zxfw/ofd.html?unid=" + this.docunid);
                  }
                  if (
                    filePath.toLowerCase().indexOf(".pdf") != -1 ||
                    fileName.toLowerCase().indexOf(".pdf") != -1
                  ) {
                    window.open("/showpdf?unid=" + file.unid);
                  }
                }
              } else {
                if (filePath == null) {
                  editZBToGZDocV2(
                    this.baseURL,
                    "",
                    item.unid,
                    this.docunid,
                    "",
                    "",
                    this.bookmark,
                    "",
                    this.moduleName,
                    this.userInfo.personCommonname,
                    this.ycHongTou
                  );
                } else {
                  editGZDocV2(
                    this.baseURL,
                    "",
                    item.unid,
                    this.docunid,
                    "",
                    "",
                    this.bookmark,
                    "",
                    this.moduleName,
                    this.userInfo.personCommonname,
                    this.ycHongTou
                  );
                }
              }
            } else {
              //不存在打开正文，并添加保存标识
              getWordFilesList({
                parentUnid: this.docunid
              }).then(res => {
                if (res.code === 200) {
                  if (res.result && res.result.data.length > 0) {
                    var item = res.result.data[res.result.data.length - 1];
                    editGWToGZDocV2(
                      this.baseURL,
                      "",
                      item.unid,
                      this.docunid,
                      "",
                      "",
                      this.bookmark,
                      "",
                      this.moduleName,
                      this.userInfo.personCommonname,
                      this.ycHongTou
                    );

                    var that = this;
                    //定时器每3秒发送一次请求
                    var interval = setInterval(function () {
                      that.getGaiZhangDoc();
                      if (that.hasGaiZhangDoc) {
                        clearInterval(interval);
                      }
                    }, 3000);
                  } else {
                    this.$message.error("未上传正文，请先上传正文");
                  }
                }
              });
            }
          } else {
            this.$message.error("未找到文件");
          }
        })
        .catch(err => {
          console.log(err);
          this.$message.error("未找到文件");
        });
    },
    //下载正文
    downloadDoc() {
      getWordFilesList({
        parentUnid: this.docunid
      }).then(res => {
        if (res.code === 200) {
          if (res.result && res.result.data.length > 0) {
            var item = res.result.data[res.result.data.length - 1];
            const url = getDownloadZBUrl(item.unid);
            window.open(url);
          }
        }
      });
    },
    //下载盖章正文
    downloadGaiZhangDoc(unid) {
      const url = downWordFile(unid);
      window.open(url);
    },
    writeZhiBanDoc() {

      //获取盖章文件
      getFileDataList({
        parentUnid: this.docunid
      })
        .then(res => {
          if (res.code == 200) {
            if (res.result && res.result.data.length > 0) {
              //存在，打开fileData盖章文伯
              var item = res.result.data[res.result.data.length - 1];
              editZhiBanDocV2(
                this.baseURL,
                "",
                item.unid,
                this.docunid,
                "",
                "",
                this.bookmark,
                "",
                this.moduleName,
                this.userInfo.personCommonname,
                this.ycHongTou
              );






            } else {
              //不存在打开正文，并添加保存标识
              getWordFilesList({
                parentUnid: this.docunid
              }).then(res => {
                if (res.code === 200) {
                  if (res.result && res.result.data.length > 0) {
                    var item = res.result.data[res.result.data.length - 1];

                    editGwToZhiBanDocV2(
                      this.baseURL,
                      "",
                      item.unid,
                      this.docunid,
                      "",
                      "",
                      this.bookmark,
                      "",
                      this.moduleName,
                      this.userInfo.personCommonname,
                      this.ycHongTou
                    );
                  } else {
                    this.$message.error("未上传正文，请先上传正文");
                  }
                }
              });
            }
          } else {
            this.$message.error("未找到文件");
          }
        })
        .catch(err => {
          this.$message.error("未找到文件");
        });
    },
    //查看公文传输的查看正文
    gwcsCkzw() {
      console.log("进入查看公文传输的查看正文")
      var that = this;
      getWordFilesList({
        parentUnid: this.docunid
      })
        .then(res => {
          if (res.code == 200) {
            if (res.result && res.result.data.length > 0) {

              //存在，打开fileData盖章文伯
              var item = res.result.data[res.result.data.length - 1];

              console.log(item.attachName);
              console.log("----dm---")
              console.log(item.attachName.toLowerCase().indexOf(".ofd") > -1);

              if (item.attachName.indexOf(".ofd") > -1) {
                var that = this;
                var windowObj = window.open(
                  "/zxfw/ofdgwcs.html?unid=" + item.unid
                );
                // var loop = setInterval(function() {
                //   if (windowObj != null && windowObj.closed) {
                //     clearInterval(loop);
                //     window.location.reload();
                //   }
                // }, 800);
              } else {

                viewDocument(
                  this.baseURL,
                  item.unid,
                  ""
                );
              }


            } else {
              editZhiBanDocV2(
                that.baseURL,
                "",
                item.unid,
                that.docunid,
                "",
                "",
                that.bookmark,
                "",
                that.moduleName,
                that.userInfo.personCommonname,
                that.ycHongTou
              );
            }
          } else {
            this.$message.error("未找到文件");
          }
        })
        .catch(err => {
          console.log(err)
          this.$message.error("未找到文件");
        });
    },
    //查看正文
    writeDoc() {
      //this.flowButtonState.btnZhiBan == true 如果是制版，打开正文（editGwDocV2）
      //如果没有编辑权限，则直接打开
      //获取最新的正文文件
      if (typeof this.docunid == 'undefined' || this.docunid == "") {
        return;
      }

      var that = this;
      getWordFilesList({
        parentUnid: this.docunid
      })
        .then(res => {
          if (res.code === 200) {
            var item;
            if (res.result && res.result.data.length > 0) {
              item = res.result.data[res.result.data.length - 1];
            }
            if (that.readOnly === true || (typeof item != 'undefined' && item.state == 1)) {
              if (res.result && res.result.data.length > 0) {
                //打开只读正文
                viewDocument(
                  that.baseURL,
                  item.unid,
                  ""
                );
                if (item.state == 1) {
                  that.$message.warning("文件已被 “" + item.lockUserName + "” 锁定，以只读方式打开");
                }
              } else {
                that.$message.info("很抱歉，该文件没有正文");
              }
            } else {
              let hiddenBtnList = "";
              if (that.flowButtonState.btnZhiBan === false) {
                hiddenBtnList = ",btnInsertRedHeader";
              }
              let ShowRevision = false;
              if (that.flowButtonState.XxMrxh === true) {
                ShowRevision = true;
              }
              //是否
              if (that.isCurrentFlow || !that.$route.query.unid) {
                if (res.result && res.result.data.length > 0) {
                  var item = res.result.data[res.result.data.length - 1];
                  editGwDocV2(
                    that.baseURL,
                    "",
                    item.unid,
                    that.docunid,
                    "",
                    "",
                    that.bookmark,
                    hiddenBtnList,
                    that.moduleName,
                    that.userInfo.personCommonname,
                    ShowRevision,
                    that.ycHongTou,
                    that.sealData
                  );
                } else {
                  editGwDocV2(
                    that.baseURL,
                    "",
                    "",
                    that.docunid,
                    "",
                    "",
                    that.bookmark,
                    hiddenBtnList,
                    that.moduleName,
                    that.userInfo.personCommonname,
                    ShowRevision,
                    that.ycHongTou,
                    that.sealData
                  );
                }
              } else {
                if (res.result && res.result.data.length > 0) {
                  var item = res.result.data[res.result.data.length - 1];
                  editGwDocV2(
                    that.baseURL,
                    "",
                    item.unid,
                    that.docunid,
                    "",
                    "",
                    that.bookmark,
                    hiddenBtnList,
                    that.moduleName,
                    that.userInfo.personCommonname,
                    ShowRevision,
                    that.ycHongTou,
                    that.sealData
                  );
                } else {
                  this.$message.info("很抱歉，该文件没有正文");
                }
              }
            }
          } else {
            this.$message.error("未找到文件");
          }
        })
        .catch(err => {
          this.$message.error("未找到文件");
        });
    },
    //打印
    print() {
      console.log(this.moduleName, this.docunid);
      let bt = this.moduleName;
      this.printLoading = true;
      if (this.moduleName == "fwgl") bt = "发文处理单";
      if (this.moduleName == "swgl") bt = "收文处理单";
      if (this.moduleName == "wcgl") bt = "外出报备";
      if (this.moduleName == "ldrc") bt = "领导日程";
      if (this.moduleName == "qjgl") bt = "请假管理";
      if (this.moduleName == "clgl") bt = "车辆管理";
      if (this.moduleName == "xxsp") bt = "信息审批";
      bbglPrint({ bt: bt, unid: this.docunid,moduleName:this.moduleName })
        .then(res => {
          let fileName = bt + ".docx";
          if (res.type == "application/pdf") {
            fileName = bt + ".pdf";
          }
          this.download(res, fileName);
        })
        .finally(() => {
          this.printLoading = false;
        });
      console.log("print");
    },
    // 打开附件
    openAnnx() {
      this.$refs.roup.show();
    },
    //打开指定unid的word原稿
    openRawZw(unid) {

      //打开原稿
      //获取正文并打开
      getWordFilesList({
        parentUnid: unid
      })
        .then((res) => {
          if (res.code === 200) {
            if (res.result && res.result.data.length > 0) {
              var item = res.result.data[res.result.data.length - 1];
              viewDocument(
                this.baseURL,
                item.unid,
                ""
              );
            } else {
              this.$message.info("很抱歉，该文件没有正文")
            }
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //打开指定unid的改稿正文
    openGGZw(unid) {
      //打开改稿痕迹
      //获取正文并打开
      getWordFilesList({
        parentUnid: unid
      })
        .then((res) => {
          if (res.code === 200) {
            if (res.result && res.result.data.length > 0) {
              var item = res.result.data[res.result.data.length - 1];
              viewDocument(
                this.baseURL,
                item.unid,
                ""
              );
            } else {
              this.$message.info("很抱歉，该文件没有正文")
            }
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfo;
    }
  },
  watch: {
  }
};
</script>

<style scoped>
.flowButtonView .ant-btn {
  margin-left: 20px;
}
</style>
